using System.Text;
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;
using BA.DAL;
using BA;

namespace BlazeApps.Library.Page
{
    [Serializable()]
    public class Ranks : Csla.BusinessBase<Ranks>
    {
        private BlazeApps.Library.ConfigSettings Config = new BlazeApps.Library.ConfigSettings();
        private BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
        private Procedures Procs = new Procedures();

        #region Business Properties and Methods

        //declare members
        private Guid _id = Guid.NewGuid();
        private Guid _itemid = Guid.NewGuid();
        private Guid _userid = Guid.NewGuid();
        private int _rank = 0;
        private int _revision = 0;
        private Boolean _hasdata = false;

        [System.ComponentModel.DataObjectField(true, true)]
        public Guid id
        {
            get
            {
                CanReadProperty("id", true);
                return _id;
            }
        }

        public Guid itemid
        {
            get
            {
                CanReadProperty("itemid", true);
                return _itemid;
            }
            set
            {
                CanWriteProperty("itemid", true);
                if (!_itemid.Equals(value))
                {
                    _itemid = value;
                    PropertyHasChanged("itemid");
                }
            }
        }

        public Guid userid
        {
            get
            {
                CanReadProperty("userid", true);
                return _userid;
            }
            set
            {
                CanWriteProperty("userid", true);
                if (!_userid.Equals(value))
                {
                    _userid = value;
                    PropertyHasChanged("userid");
                }
            }
        }

        public int rank
        {
            get
            {
                CanReadProperty("rank", true);
                return _rank;
            }
            set
            {
                CanWriteProperty("rank", true);
                if (!_rank.Equals(value))
                {
                    _rank = value;
                    PropertyHasChanged("rank");
                }
            }
        }

        public int revision
        {
            get
            {
                CanReadProperty("revision", true);
                return _revision;
            }
            set
            {
                CanWriteProperty("revision", true);
                if (!_revision.Equals(value))
                {
                    _revision = value;
                    PropertyHasChanged("revision");
                }
            }
        }

        public Boolean hasdata
        {
            get
            {
                return _hasdata;
            }
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        #endregion //Business Properties and Methods

        #region Validation Rules
        private void AddCustomRules()
        {
            //add custom/non-generated rules here...
        }

        private void AddCommonRules()
        {
            ////
            //// createdate
            ////
            //ValidationRules.AddRule(CommonRules.StringRequired, "createdateString");
        }

        protected override void AddBusinessRules()
        {
            AddCommonRules();
            AddCustomRules();
        }
        #endregion //Validation Rules

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
            //TODO: Define authorization rules in Ranks
            //AuthorizationRules.AllowRead("id", "RanksReadGroup");
            //AuthorizationRules.AllowRead("itemid", "RanksReadGroup");
            //AuthorizationRules.AllowRead("userid", "RanksReadGroup");
            //AuthorizationRules.AllowRead("rank", "RanksReadGroup");
            //AuthorizationRules.AllowRead("IvAccesstype", "RanksReadGroup");
            //AuthorizationRules.AllowRead("revision", "RanksReadGroup");
            //AuthorizationRules.AllowRead("views", "RanksReadGroup");
            //AuthorizationRules.AllowRead("createdate", "RanksReadGroup");

            //AuthorizationRules.AllowWrite("userid", "RanksWriteGroup");
            //AuthorizationRules.AllowWrite("rank", "RanksWriteGroup");
            //AuthorizationRules.AllowWrite("IvAccesstype", "RanksWriteGroup");
            //AuthorizationRules.AllowWrite("revision", "RanksWriteGroup");
            //AuthorizationRules.AllowWrite("views", "RanksWriteGroup");
            //AuthorizationRules.AllowWrite("createdate", "RanksWriteGroup");
        }


        public static bool CanGetObject()
        {
            //TODO: Define CanGetObject permission in Ranks
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("RanksViewGroup"))
            //	return true;
            //return false;
        }

        public static bool CanAddObject()
        {
            //TODO: Define CanAddObject permission in Ranks
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("RanksAddGroup"))
            //	return true;
            //return false;
        }

        public static bool CanEditObject()
        {
            //TODO: Define CanEditObject permission in Ranks
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("RanksEditGroup"))
            //	return true;
            //return false;
        }

        public static bool CanDeleteObject()
        {
            //TODO: Define CanDeleteObject permission in Ranks
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("RanksDeleteGroup"))
            //	return true;
            //return false;
        }
        #endregion //Authorization Rules

        #region Factory Methods
        private Ranks()
        { /* require use of factory method */
        }

        public static Ranks NewRanks()
        {
            if (!CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a Ranks");
            return DataPortal.Create<Ranks>();
        }

        public static Ranks GetRanks(Guid id)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a Ranks");
            return DataPortal.Fetch<Ranks>(new Criteria(id));
        }

        public static Ranks GetRanks(Guid itemid, Guid userid, int revision)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a Ranks");
            return DataPortal.Fetch<Ranks>(new Criteria(itemid, userid, revision));
        }

        public static void DeleteRanks(Guid id)
        {
            if (!CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a Ranks");
            DataPortal.Delete(new Criteria(id));
        }

        public override Ranks Save()
        {
            if (IsDeleted && !CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a Ranks");
            else if (IsNew && !CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a Ranks");
            else if (!CanEditObject())
                throw new System.Security.SecurityException("User not authorized to update a Ranks");

            return base.Save();
        }

        #endregion //Factory Methods

        #region Data Access

        #region Criteria

        [Serializable()]
        private class Criteria
        {
            public Guid id;
            public Guid itemid;
            public Guid userid;
            public int revision;

            public Criteria(Guid id)
            {
                this.id = id;
            }

            public Criteria(Guid itemid, Guid userid, int revision)
            {
                this.itemid = itemid;
                this.userid = userid;
                this.revision = revision;
            }
        }

        #endregion //Criteria

        #region Data Access - Create
        [RunLocal]
        protected override void DataPortal_Create()
        {
            ValidationRules.CheckRules();
        }

        #endregion //Data Access - Create

        #region Data Access - Fetch
        private void DataPortal_Fetch(Criteria criteria)
        {
            ExecuteFetch(criteria);
        }

        private void ExecuteFetch(Criteria criteria)
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddFetchParameters(ref DAL, criteria);

            using(SafeDataReader dr = new SafeDataReader(DAL.ExecuteReader(Procs.GetRankByItemID.ToString().Trim(), CommandType.Text, ConnectionState.Open)))
            {
                FetchObject(dr);
                ValidationRules.CheckRules();

                //load child object(s)
                FetchChildren(dr);
            }
        }

        private void AddFetchParameters(ref BA.DAL.DBUtility DAL, Criteria criteria)
        {
            DAL.AddParameter("@itemid", criteria.itemid);
            DAL.AddParameter("@revision", criteria.revision);
            DAL.AddParameter("@userid", criteria.userid);
        }

        private void FetchObject(SafeDataReader dr)
        {
            if (dr.Read())
            {
                _rank = dr.GetInt32("rank");
                _hasdata = true;
            }
            else
            {
                _hasdata = false;
            }
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }
        #endregion //Data Access - Fetch

        #region Data Access - Insert
        protected override void DataPortal_Insert()
        {
            ExecuteInsert();
        }

        private void ExecuteInsert()
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddInsertParameters(ref DAL);

            DAL.ExecuteNonQuery(Procs.InsertRank.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddInsertParameters(ref BA.DAL.DBUtility DAL)
        {

            DAL.AddParameter("@id", _id);
            DAL.AddParameter("@itemid", _itemid);
            DAL.AddParameter("@userid", _userid);
            DAL.AddParameter("@rank", _rank);
            DAL.AddParameter("@revision", _revision);
        }
        #endregion //Data Access - Insert

        #region Data Access - Update
        //protected override void DataPortal_Update()
        //{
        //    if (base.IsDirty)
        //    {
        //        ExecuteUpdate();
        //    }
        //}

        //private void ExecuteUpdate()
        //{
        //    BlazeApps.Library.ConfigSettings Config = new BlazeApps.Library.ConfigSettings();
        //    BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
        //    DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

        //    AddUpdateParameters(ref DAL);

        //    DAL.ExecuteNonQuery("usp_update_page", CommandType.StoredProcedure, ConnectionState.Open);
        //}

        //private void AddUpdateParameters(ref BA.DAL.DBUtility DAL)
        //{
        //    DAL.AddParameter("@id", _id);
        //    DAL.AddParameter("@itemid", _itemid);
        //    if (_userid != string.Empty)
        //        DAL.AddParameter("@userid", _userid);
        //    else
        //        DAL.AddParameter("@userid", DBNull.Value);
        //    DAL.AddParameter("@rank", _rank);
        //    DAL.AddParameter("@iv_accesstype", _ivAccesstype);
        //    DAL.AddParameter("@revision", _revision);
        //    DAL.AddParameter("@views", _views);
        //}

        //private void UpdateChildren(SqlConnection cn)
        //{
        //}
        #endregion //Data Access - Update

        #region Data Access - Delete
        //protected override void DataPortal_DeleteSelf()
        //{
        //    DataPortal_Delete(new Criteria(_id));
        //}

        //private void DataPortal_Delete(Criteria criteria)
        //{
        //    using (SqlConnection cn = new SqlConnection(Database.BlazeKBConnection))
        //    {
        //        cn.Open();

        //        ExecuteDelete(cn, criteria);

        //    }//using

        //}

        //private void ExecuteDelete(SqlConnection cn, Criteria criteria)
        //{
        //    using (SqlCommand cm = cn.CreateCommand())
        //    {
        //        cm.CommandType = CommandType.StoredProcedure;
        //        cm.CommandText = "DeleteRanks";

        //        cm.Parameters.AddWithValue("@id", criteria.id);

        //        cm.ExecuteNonQuery();
        //    }//using
        //}
        #endregion //Data Access - Delete
        #endregion //Data Access
    }
}
